package org.Lin.service;

import org.Lin.dao.FriendMessageDAO;
import org.Lin.model.FriendMessage;
import org.Lin.util.LogUtil;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

public class FriendMessageService {

    private final FriendMessageDAO friendMessageDAO = new FriendMessageDAO();

    public boolean sendPrivateMessage(int senderId, int receiverId, String messageData) {
        try {
            FriendMessage msg = new FriendMessage();
            msg.setSenderId(senderId);
            msg.setReceiverId(receiverId);
            msg.setMessageData(messageData);
            msg.setSentTime(LocalDateTime.now());
            msg.setMessageStatus("正常");
            friendMessageDAO.insert(msg);

            LogUtil.logOperation("FriendMessage", "send", "发送私聊消息给 " + receiverId + ": " + messageData);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<FriendMessage> getMessagesBetween(int senderId, int receiverId) throws SQLException {
        return friendMessageDAO.findBySenderAndReceiver(senderId, receiverId);
    }
}
